/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * https://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */

package com.dian.modules.oss.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dian.common.exception.RRException;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.utils.R;
import com.dian.modules.oss.cloud.OSSFactory;
import com.dian.modules.oss.entity.SysOssEntity;
import com.dian.modules.oss.service.SysOssService;
import com.dian.modules.oss.dao.SysOssDao;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;


@Service("sysOssService")
public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> implements SysOssService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<SysOssEntity> page = this.page(
			new Query<SysOssEntity>().getPage(params)
		);

		return new PageUtils(page);
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}

		//上传文件
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

		//保存文件信息
		SysOssEntity ossEntity = new SysOssEntity();
		ossEntity.setName(file.getOriginalFilename());
		ossEntity.setUrl(url);
		ossEntity.setCreateTime(new Date());
		save(ossEntity);

		return url;
	}

}
